layui.extend({ //设定组件别名
	formSelects: '/statics/libs/formSelects/formSelects-v4.min',
	soulTable: '/statics/libs/layuiExt/soulTable/soulTable',
	tableChild: '/statics/libs/layuiExt/soulTable/tableChild',
	tableMerge: '/statics/libs/layuiExt/soulTable/tableMerge',
	tableFilter: '/statics/libs/layuiExt/soulTable/tableFilter',
	excel: '/statics/libs/layuiExt/soulTable/excel',
	locals: '/statics/libs/layuiExt/localsession/locals',
    step: '/statics/libs/layuiExt/step-lay/step',
});
//对象数组的去重
function uniqObjInArray(objarray) {
	let len = objarray.length;
	let tempJson = {};
	let res = [];
	for (let i = 0; i < len; i++) {
		//取出每一个对象
		tempJson[JSON.stringify(objarray[i])] = true;
	}
	let keyItems = Object.keys(tempJson);
	for (let j = 0; j < keyItems.length; j++) {
		res.push(JSON.parse(keyItems[j]));
	}
	return res;
}
// 获取pageName
function getPageName(){
	var pathname = window.location.pathname;
	if(pathname == "/"){
		return 'monitor'
	}else{
		return pathname.split('/')[1]
	}
}

function initMenu() {
	$(".navbar-nav li").each(function () {
		var authCode=$(this).attr("auth");
		if(!hasAuth(authCode)){
			$(this).hide();
		}
	});
}
function hasAuth(authCode) {
	var authArray=authStr.split(",");
	if(authArray&&authArray.length>0){
		for(var i=0;i<authArray.length;i++){
			if(authArray[i].trim()==authCode){
				return true;
			}
		}
	}
	return false;
}

$("body").on("click","#logout",function(){
	$.ajax({
		type:"post",
		url: ctx+"/logout.do",
		data:{},
		async:	true,
		cache: false,
		dataType:"json",
		beforeSend:function(){
			//layer.load(2);
		},success:function(data){
			if(data.code=="000000"){
				location.reload();
			}else{
				alert("系统异常，"+data.message);
			}
		},error:function(e){
			alert("系统异常，请稍后再试");
		},complete:function(){
			//layer.closeAll('loading');
		}
	});
});

//导航高亮
function navActive() {
	var pathname = window.location.pathname;
	var activeNav = $("#mynav li a[href='"+ pathname +"']");
	activeNav.parent("li").addClass("active");
}
//当天日期，格式：2019-01-01
function formatToday() {
	var date = new Date();
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	return y + '-' + m + '-' + d;
};

//当天日期，格式：2019-01-01
function formatYesterday() {
	var date = new Date();
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
	if(d!=1){
         d = date.getDate() - 1;
    }
	d = d < 10 ? ('0' + d) : d;
	return y + '-' + m + '-' + d;
};

//当月1号日期，格式：2019-01-01
function format01day() {
	var date = new Date();
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = "01";
	return y + '-' + m + '-' + d;
};

//数组求和
function sumArray(array){
	var sum = 0;
	array.forEach(function(val,index,array){
		sum += val;
	});
	return sum;
}
//初始化日期，X号之前向前推n天，X号以后默认当月1号
function initStartDate(x, n) {
	x = x || 3;
	n = n || 3;
	var today = new Date();
	var preDate;
	var dd = today.getDate();
	if (dd <= x) {
		preDate = new Date(today.getTime() - n * 24 * 60 * 60 * 1000); //前n天
	} else {
		preDate = new Date(today.getTime() - (dd - 1) * 24 * 60 * 60 * 1000); //当月1号
	}
	return preDate;
}

/*计算每个月有多少天
parm: date '2020-01-01'
*/
function getAMonthDays(year, month) {
    var days = new Date(year, month, 0).getDate();
    return days
}

/**
 * 根据年份计算总周数
 * @param year
 * @returns {number}
 */
function getNumOfWeeks(year) {
    //设置为这一年开始日期
    var startDateOfYear = new Date(year, 0, 1);
    //计算这一年有多少天
    var daysOfYear = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 366 : 365;
    //366（365）/7=52.2(52.1)，所以一般一年有52周余1天或者2天，当这一年有366天时且第一天是周日，那么他的最后一天则是周一，这一年就有54周。
    var weekNum = 53;
    //当年份是闰年且第一天是周日时有54周
    if (startDateOfYear.getDay() == 0 && daysOfYear == 366) {
        weekNum = 54;
    }
    return weekNum;
}

/* 获取某天所在周的起始、结束日期
 * **/
function getWeekStartEnd(now) {
    var now = new Date(now) || new Date();
    var nowTime = now.getTime();
    var day = now.getDay();
    var longTime = 24 * 60 * 60 * 1000; // 24小时
    var arr = [];
    var start = nowTime - (day - 1) * longTime - longTime;
    arr.push(start);
    var end = nowTime + (7 - day) * longTime - longTime;
    arr.push(end);
    return arr;
};

/* 对象数组排序
*  key 要排序的属性
* */
function compare(key) {
    return function (value1, value2) {
        var val1 = value1[key];
        var val2 = value2[key];
        return val1 < val2 ? -1 : 0;
    }
}
/* 用量保留小数位 */
function decimalNum(num) {
    return parseFloat((num).toFixed(1));
}

/* 设置开始时间：
* parm ： type
*  1日： 当天0点
*  2周： 当周第一天（周日为起始，周六为结束）
*  3月：当月1号
*  4年： 本年
*   return [st, et]
*  */
function defaultSTimeETime(timeType, type, date) {
    var st = '', et = '';
    var nowDate = date == undefined ? new Date() : new Date(date);
    switch (type) {
        case '2':
            if(timeType === 0){ // 0~0
                st = layui.util.toDateString(nowDate, 'yyyy-MM-dd') + ' 00:00:00';
                et = layui.util.toDateString(nowDate, 'yyyy-MM-dd') + ' 23:59:59';
            }else if(timeType === 1){ // 8~8
                let temp = new Date(nowDate.getTime() + 1000 * 60 * 60 *24);
                st = layui.util.toDateString(nowDate, 'yyyy-MM-dd') + ' 08:00:00';
                et = layui.util.toDateString(temp, 'yyyy-MM-dd') + ' 07:59:59';
            }
            break;
        case '3':
            var week = getWeekStartEnd(nowDate);
            st = layui.util.toDateString(week[0], 'yyyy-MM-dd');
            et = layui.util.toDateString(week[1], 'yyyy-MM-dd');
            break;
        case '4':
            var year = nowDate.getFullYear();
            var month = nowDate.getMonth() + 1;
            st = layui.util.toDateString(nowDate, 'yyyy-MM') + '-01';
            et = layui.util.toDateString(nowDate, 'yyyy-MM') + '-' + getAMonthDays(year, month);
            break;
        case '5':
            st = layui.util.toDateString(nowDate, 'yyyy');
            et = layui.util.toDateString(nowDate, 'yyyy');
            break;
    }
    return [st, et];
}
